<?php

namespace App\Services\Sign;

use App\Constants\CodeData;
use App\Dao\BackpackDao;
use App\Dao\SettingDao;
use App\Model\SignDraw;
use App\Model\UserSign;
use Hyperf\DbConnection\Db;
use function Hyperf\Config\config;
use function Hyperf\Translation\trans;

class SignService
{

    /**
     * Note - 签到列表(初始化签到数据)
     * DateTime 2024/8/12 12:29
     * User Admin
     * @param $uid
     * @param $language
     * @return array
     */
    public function signList($uid, $language)
    {
        $setting = SettingDao::getInstance();
        if (intval($setting->getOne('user_sign_switch')) == 0) {
            return formatData([], 200, CodeData::NEED_TIPS, '每日签到未开启');
        }

        $draw_model = new SignDraw();
        $reward = $draw_model->getListByKindType(0);
        if (!$reward) {
            $reward = config('custom.sign_reward');
        }
        foreach ($reward as $k => $v) {
            $reward[$k]['name'] = trans('message.' . $v['name'], [], $language);
        }
        $data = UserSign::query()->where('uid', $uid)->first();
        $is_sign_today = 0;
        if (empty($data)) {
            $data['uid'] = $uid;
            $data['sign_total'] = 0;
            UserSign::query()->insert($data);
            $data['sign_last_date'] = 0;
        } else {
            if (date('Y-m-d') == $data['sign_last_date']) {
                $is_sign_today = 1;
            } else if ($data['sign_total'] == 7 || date("Y-m-d", strtotime("-1 day")) != $data['sign_last_date']) {
                // $model->updateByWhere(['sign_total' => 0], 'uid =' . $uid);
                UserSign::query()->where('uid', $uid)->update(['sign_last_date' => date('Y-m-d')]);
                $data['sign_total'] = 0;
            }
        }
        $data['is_sign_today'] = $is_sign_today;
        $data['reward'] = $reward;
        $cb['result'] = 1;
        $cb['msg'] = $data;
        return formatData($cb, 200, CodeData::SUCCESS, '操作成功');
        // Tool::JSON(\CodeData::SUCCESS, $cb, '操作成功');
    }


    /**
     * Note - 签到(签到)
     * DateTime 2024/8/12 12:55
     * User Admin
     * @param $uid
     * @param $language
     * @param $params
     * @return array
     */
    public function sign($uid, $language, $params)
    {
        $setting = SettingDao::getInstance();
        if (intval($setting->getOne('user_sign_switch')) == 0) {
            return formatData([], 200, CodeData::NEED_TIPS, '每日签到未开启');
        }

        $draw_model = new SignDraw();
        $reward = $draw_model->getListByKindType(0);
        if (!$reward) {
            $reward = config('custom.sign_reward');
        }
        foreach ($reward as $k => $v) {
            $reward[$k]['name'] = trans('message.' . $v['name'], [], $language);
        }

        $data = UserSign::query()->where('uid', $uid)->first();
        $backpack = BackpackDao::getInstance();
        if (!empty($data)) {
            if (date('Y-m-d') == $data['sign_last_date']) {
                // Tool::JSON(\CodeData::SUCCESS, array('result' => 3,'msg'=>array()), '您今天已经签到过了');
                return formatData([], 200, CodeData::NEED_TIPS, '您今天已经签到过了');
            } else if ($data['sign_total'] == 7 || date("Y-m-d", strtotime("-1 day")) != $data['sign_last_date']) {
                $sign_total = 1;
            } else {
                $sign_total = $data['sign_total'] + 1;
            }
            // $model->updateByWhere(['sign_total'=>$sign_total,'sign_last_date'=>date('Y-m-d')],'uid ='.$uid);
            Db::transaction(function () use ($uid, $sign_total, $data, $reward, $backpack, $params) {
                UserSign::query()->where('uid', $uid)->update(['sign_last_date' => date('Y-m-d'), 'sign_total' => $sign_total]);
                $backpack->reward($uid, $reward[$sign_total - 1], $params);
            });
            // Tool::JSON(\CodeData::SUCCESS, array('result' => 1,'msg'=>array('reward'=>$reward[$sign_total-1])), '签到成功');
            return formatData([], 200, CodeData::SUCCESS, '签到成功');
        } else {
            // Tool::JSON(\CodeData::NEED_TIPS, '', '网络繁忙，请重新提交');
            return formatData([], 200, CodeData::NEED_TIPS, '网络繁忙，请重新提交');
        }
    }
}